<?php
session_save_path('sessions');
session_start();

include("include/dbUserTypes.php");

// Include our user class
include_once("include/user.php");

// Create a variable to store our user if we're validated
$user = null;

// Check if we're in a valid session. If not, go back to main login page.

if( !isset($_SESSION['user']) || $_SESSION['user'] == null ) {
    header("location:main_login.php");
}
else {
    // Unserialize our user to turn it back into a useful object
    $user = unserialize($_SESSION['user']);
}

// If we're not a business (or admin) go back to the user redirection page
if( $user->getUserType() != $USERTYPE_REGISTERED && $user->getUserType() != $USERTYPE_ADMIN)
    header("location:login_success.php");



// Include the header (CSS files, etc)
include("include/dbconnection.php");
include("include/header.php"); ?>

 
<body>
  <div id="wrapper">
    <div id="header"> </div>
    <div id="left">
      <div id="logo">
        <h1>Home Sale</h1>
        <p>Only By Owner</p>
      </div>
      <div id="nav">
        <ul>
          <li >
            <a href="addAhomeForSale.php">Add a House for sale</a>
          </li>
          <li class="important">
            <a href="updateAhome.php">Update House information</a>
          </li>
          <li >
            <a href="updateAhomePicture.php">Update House picture</a>
          </li>
          <li>
            <a href="searchhouse.php">Search House</a>
          </li>
          <li>
            <a href="emailMenu.php">Mailbox</a>
          </li>
          <li>
            <a href="logout.php">Logout</a>
          </li>
        </ul>
      </div>
      <div id="news">
        <?php include("adgeneratorleft1.php");?>
        <div class="hr-dots"> </div>
        <?php include("adgeneratorleft2.php");?>
        <p class="more"><a href="registered_index.php">more</a></p>
      </div>
      <div id="support">
        
      </div>
    </div>
    <div id="right">
      <h2>House information was updated</h2>
      <div id="welcome">
		<p>Current information for this house:</p>

		<?php
			if(!isset($_GET['homeID']))
				{
					$address = $_POST["address"];
					$streetName = $_POST["streetName"];
					$city = $_POST["city"];
					$country = $_POST["country"];
					$district = $_POST["district"];
					$zone = $_POST["zone"];
					$postalCode = $_POST["postalCode"];
					$lotSize = $_POST["lotSize"];
					$propertyType = $_POST["propertyType"];
					$yearBuilt = $_POST["yearBuilt"];
					$heating = $_POST["heating"];
					$cooling = $_POST["cooling"];
					$parking = $_POST["parking"];
					$garage = $_POST["garage"];
					$driveway = $_POST["driveway"];
					$neighbourhood = $_POST["neighbourhood"];
					$recentImprovements = $_POST["recentImprovements"];
					$evaluation = $_POST["evaluation"];
					$taxes = $_POST["taxes"];
					$utilityCost = $_POST["utilityCost"];
					$price = $_POST["price"];
					$comments = $_POST["comments"];
					$listingDate = date("U");
					$homeID = $_POST['homeID'];
				}
			else
				{
				$homeID = $_GET['homeID'];
				$getHomeInfo = mysql_query("select * from home where homeID = $homeID");
					while( $row3 = mysql_fetch_array($getHomeInfo) ) {

						$address = $row3[$TABLE_HOME_ADDRESS];
						$streetName = $row3[$TABLE_HOME_STREETNAME];
						$city = $row3[$TABLE_HOME_CITY];
						$country = $row3[$TABLE_HOME_COUNTRY];
						$district = $row3[$TABLE_HOME_DISTRICT];
						$zone = $row3[$TABLE_HOME_ZONE];
						$postalCode = $row3[$TABLE_HOME_POSTALCODE];
						$lotSize = $row3[$TABLE_HOME_LOTSIZE];
						$propertyType = $row3[$TABLE_HOME_PROPERTYTYPE];
						$yearBuilt = $row3[$TABLE_HOME_YEARBUILT];
						$heating = $row3[$TABLE_HOME_HEATING];
						$cooling = $row3[$TABLE_HOME_COOLING];
						$parking = $row3[$TABLE_HOME_PARKING];
						$garage = $row3[$TABLE_HOME_GARAGE];
						$driveway = $row3[$TABLE_HOME_DRIVEWAY];
						$neighbourhood = $row3[$TABLE_HOME_NEIGHBOURHOOD];
						$recentImprovements = $row3[$TABLE_HOME_IMPROVEMENTS];
						$evaluation = $row3[$TABLE_HOME_EVALUATION];
						$taxes = $row3[$TABLE_HOME_TAXES];
						$utilityCost = $row3[$TABLE_HOME_UTILITYCOST];
						$price = $row3[$TABLE_HOME_PRICE];
						$comments = $row3[$TABLE_HOME_COMMENTS];
						$listingDate = $row3[$TABLE_HOME_LISTINGDATE];
					}
				}


			$sql=	"UPDATE home 
						SET			address = $address, streetName = '$streetName', city = '$city', country = '$country', district = '$district', zone = '$zone', 
									postalCode = '$postalCode', lotSize = $lotSize, propertyType = $propertyType, yearBuilt = $yearBuilt,heating = $heating, cooling = $cooling, 
									parking = $parking, garage = $garage, driveway = $driveway, neighbourhood = '$neighbourhood', recentImprovements = '$recentImprovements', 
									evaluation = $evaluation, taxes = $taxes, utilityCost = $utilityCost, price = $price, comments = '$comments', listingDate = '$listingDate'
						Where homeID = $homeID";

			if (!mysql_query($sql))
			  {
			  die('Error: ' . mysql_error());
			  }

			
			$result = mysql_query("select * from home where homeID = $homeID");
			
				// Show all the users and add an activation link.
				while( $row = mysql_fetch_array($result) ) {
					?>



	<table>
    <form name="myForm" action="updateAhomeValidate.php" method="post" onsubmit="return validate()">
	<tr><td>Address:</td><td><input type="text" name="address" value = <?php echo $row[$TABLE_HOME_ADDRESS]?>></td></tr>
	<tr><td>Street Name:</td><td><input type="text" name="streetName" value = "<?php echo $row[$TABLE_HOME_STREETNAME]?>"></td></tr>
	<tr><td>City:</td><td><input type="text" name="city" value = "<?php echo $row[$TABLE_HOME_CITY]?>"></td></tr>
	<tr><td>Country:</td><td><input type="text" name="country" value = "<?php echo $row[$TABLE_HOME_COUNTRY]?>"></td></tr>
	<tr><td>District:</td><td><input type="text" name="district" value = "<?php echo $row[$TABLE_HOME_DISTRICT]?>"></td></tr>
	<tr><td>Zone:</td><td><input type="text" name="zone" value = "<?php echo $row[$TABLE_HOME_ZONE]?>"></td></tr>
	<tr><td>Postal Code:</td><td><input type="text" name="postalCode" value = "<?php echo $row[$TABLE_HOME_POSTALCODE]?>"></td></tr>
	<tr><td>Size of lot:</td><td><input type="text" name="lotSize" value = <?php echo $row[$TABLE_HOME_LOTSIZE]?>></td></tr>

		<tr><td>Type of property:</td><td>

	 <select name="propertyType">
		<option value="<?php echo $row[$TABLE_HOME_PROPERTYTYPE]?>">

        <?php 
		switch($row[$TABLE_HOME_PROPERTYTYPE]){
        case 1:
        {
          echo "Condo";
          break;
        }
        case 2:
        {
          echo "Apartment";
          break;
        }
        case 3:
        {
          echo "Single family";
          break;
        }
        case 4:
        {
          echo "Cottages";
          break;
        }
		}
        ?></option>
                    <option value="1">Change to : Condo</option>
                    <option value="2">Change to : Apartment</option>
                    <option value="3">Change to : Single family</option>
                    <option value="4">Change to : Cottage</option> 
                  </select></td></tr>

	<tr><td>Year built:</td><td><input type="text" name="yearBuilt" value = <?php echo $row[$TABLE_HOME_YEARBUILT]?>></td></tr>
	<tr><td>Heating</td></tr>
	<tr><td><input type="radio" name="heating" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_HEATING] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="heating" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_HEATING] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Cooling</td></tr>
		<tr><td><input type="radio" name="cooling" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_COOLING] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="cooling" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_COOLING] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Parking</td</tr>
			<tr><td><input type="radio" name="parking" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_PARKING] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="parking" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_PARKING] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Garage</td></tr>
		<tr><td><input type="radio" name="garage" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_GARAGE] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="garage" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_GARAGE] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Driveway</td></tr>
			<tr><td><input type="radio" name="driveway" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_DRIVEWAY] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="driveway" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_DRIVEWAY] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Neighbourhood:</td><td><input type="text" name="neighbourhood" value = "<?php echo $row[$TABLE_HOME_NEIGHBOURHOOD]?>"></td></tr>
	<tr><td>Recent Improvements:</td><td><input type="text" name="recentImprovements" value = "<?php echo $row[$TABLE_HOME_IMPROVEMENTS]?>"></td></tr>
	<tr><td>Evaluated Price:</td><td><input type="text" name="evaluation" value = <?php echo $row[$TABLE_HOME_EVALUATION]?>></td></tr>
	<tr><td>Property Taxes:</td><td><input type="text" name="taxes" value = <?php echo $row[$TABLE_HOME_TAXES]?>></td></tr>
	<tr><td>Utility Cost:</td><td><input type="text" name="utilityCost" value = <?php echo $row[$TABLE_HOME_UTILITYCOST]?>></td></tr>
	<tr><td>Price:</td><td><input type="text" name="price" value = <?php echo $row[$TABLE_HOME_PRICE]?>></td></tr>

		<tr>
			<td>Comments:</td>
			<td>
				<textarea rows="5" cols="20" name="comments" wrap="physical" style="width:327px; height:129px;">
					<?php echo $row[$TABLE_HOME_COMMENTS]?>
				</textarea>
			</td>
		</tr>
		
	<input type="hidden" name="homeID" value = <?php echo $row[$TABLE_HOME_HOMEID]?>>
	<tr><td>Listing Date:</td><td><?php echo date("d/m/Y", $row[$TABLE_HOME_LISTINGDATE])?></td></tr>
	</table>
	<br>
    <input type="submit" value="Update" />
    </form>	
				<?php
				} // End while loop
				mysql_close();
				
				?>
				</table>


		<SCRIPT>


			function is_int(input)
			{
			return !isNaN(input)&&parseInt(input)==input;
			}

			function validate()
			{
			mNv=myForm.address.value;
			if (mNv=='')
			{
			alert('The address of the house is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Address has to be a number.');
			return false;
			}
			mNv=myForm.streetName.value;
			if (mNv=='')
			{
			alert('The Street Name is a required field.');
			return false;
			}
			mNv=myForm.city.value;
			if (mNv=='')
			{
			alert('The City in which the house is a required field.');
			return false;
			}
			mNv=myForm.country.value;
			if (mNv=='')
			{
			alert('The Country in which the house is a required field.');
			return false;
			}
			mNv=myForm.district.value;
			if (mNv=='')
			{
			alert('The District in which the house is a required field.');
			return false;
			}
			mNv=myForm.zone.value;
			if (mNv=='')
			{
			alert('The zone in which the house is a required field.');
			return false;
			}
			mNv=myForm.postalCode.value;
			if (mNv=='')
			{
			alert('The Postal Code the house is a required field.');
			return false;
			}
			mNv=myForm.lotSize.value;
			if (mNv=='')
			{
			alert('The Size of lot is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Size of lot has to be a number.');
			return false;
			}
			mNv=myForm.propertyType.value;
			if (mNv=='')
			{
			alert('The Type of property is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Type of roperty has to be a number.');
			return false;
			}
			mNv=myForm.yearBuilt.value;
			if (mNv=='')
			{
			alert('The Year built is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Year built has to be a number.');
			return false;
			}
			if (!(myForm.heating[0].checked || myForm.heating[1].checked)) {
			alert('You have to specify if there is heating.');
			return false;
			}
			if (!(myForm.cooling[0].checked || myForm.cooling[1].checked)) {
			alert('You have to specify if there is cooling.');
			return false;
			}
			if (!(myForm.parking[0].checked || myForm.parking[1].checked)) {
			alert('You have to specify if there is a parking.');
			return false;
			}
			if (!(myForm.garage[0].checked || myForm.garage[1].checked)) {
			alert('You have to specify if there is a garage.');
			return false;
			}
			if (!(myForm.driveway[0].checked || myForm.driveway[1].checked)) {
			alert('You have to specify if there is a driveway.');
			return false;
			}
			mNv=myForm.neighbourhood.value;
			if (mNv=='')
			{
			alert('You have tell in which neighbourhood this house is.');
			return false;
			}
			mNv=myForm.evaluation.value;
			if (mNv=='')
			{
			alert('You have to give the Evaluated price');
			return false;
			}if (!is_int(mNv))
			{
			alert('The Evaluated price has to be a number.');
			return false;
			}
			mNv=myForm.taxes.value;
			if (mNv=='')
			{
			alert('You have to specify the Property Taxes.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Property Taxes has to be a number.');
			return false;
			}
			mNv=myForm.utilityCost.value;
			if (mNv=='')
			{
			alert('You have to specify the Utility cost.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Utility cost has to be a number.');
			return false;
			}
			mNv=myForm.price.value;
			if (mNv=='')
			{
			alert('You have to specify the the house Price.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The house Price has to be a number.');
			return false;
			}
			else
			return true;
			}
		</SCRIPT>
		
      </div>
      <h3></h3>
      <div id="profile">
        <div id="corp">
          <?php include("adgeneratorbottomleft.php");?>
        </div>
        <div id="indu">
          <?php include("adgeneratorbottomright.php");?>
        </div>
      </div>
    </div>
    <div class="clear"> </div>
    <div id="spacer"> </div>
    <div id="footer">
      <div id="copyright">
        &copy; Template taken from http://www.freewebsitetemplates.com/
      </div>
	  <div id="footerline"></div>
    </div>
	
  </div>
</body>